//
//  YWTheMostSubscribeViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/9.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWTheMostSubscribeViewController.h"
#import "TYTabButtonPagerController.h"
#import "YWSubscribeListPageViewController.h"
#import "YWHotTopListModel.h"

#import "YWSelectCollectionViewCell.h"

@interface YWTheMostSubscribeViewController ()<TYPagerControllerDataSource, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) TYTabButtonPagerController *pageController;

@property (nonatomic, strong) NSMutableArray *topListDataArray;

@property (nonatomic, strong) UIButton *selectButton;

@property (nonatomic, strong) UIView *selectedView;

@property (nonatomic, strong) UICollectionView *selectCollectionView;

@property (nonatomic, assign) NSInteger didSelected;

@end

@implementation YWTheMostSubscribeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = self.titleString;
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    self.topListDataArray = [NSMutableArray array];
    [self addPagerController];

    [self getView];

    [self getData];
}

- (void)viewWillLayoutSubviews
{
    [super viewWillLayoutSubviews];
    _pageController.view.frame = self.view.bounds;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)addPagerController
{
    _pageController = [[TYTabButtonPagerController alloc]init];
    _pageController.dataSource = self;
    _pageController.adjustStatusBarHeight = YES;
    _pageController.barStyle = TYPagerBarStyleProgressView;
    _pageController.cellSpacing = 8;
    _pageController.progressWidth = 0;
    _pageController.view.frame = self.view.bounds;
    [self addChildViewController:_pageController];
    [self.view addSubview:_pageController.view];
}

- (NSInteger)numberOfControllersInPagerController
{
    return _topListDataArray.count;
}

- (NSString *)pagerController:(TYPagerController *)pagerController titleForIndex:(NSInteger)index
{
    YWHotTopListModel *topListModel = [self.topListDataArray objectAtIndex:index];
    return topListModel.name;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index
{
    YWSubscribeListPageViewController *lpvc = [[YWSubscribeListPageViewController alloc] init];
    YWHotTopListModel *topListModel = [_topListDataArray objectAtIndex:index];
    lpvc.urlKey = topListModel.key;

    lpvc.vcNum = index;
    lpvc.passVCNumBlock = ^(NSInteger number)
    {
        _didSelected = number;
        [_selectCollectionView reloadData];
    };
    return lpvc;
}

- (void)getView
{
    [self getButton];
    [self getSelectView];
}

- (void)getButton
{
    _selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _selectButton.frame = CGRectMake(0, 0, 50 * FITWIDTH, 25 * FITWIDTH);
    [_selectButton setBackgroundImage:[UIImage imageNamed:@" 更多"] forState:UIControlStateNormal];
    [_selectButton addTarget:self action:@selector(didClickedSelectButton:) forControlEvents:UIControlEventTouchUpInside];
    _selectButton.tag = 1000;
    UIBarButtonItem *selectItemButton = [[UIBarButtonItem alloc] initWithCustomView:_selectButton];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    back.frame = CGRectMake(0, 0, 20 * FITWIDTH, 20 * FITWIDTH);
    [back setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [back addTarget:self action:@selector(didClickedBackButton:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:back];
    self.navigationItem.leftBarButtonItems = @[backButton, selectItemButton];
    
}

- (void)didClickedSelectButton:(UIButton *)sender
{
    if (_selectButton.tag == 1000) {
        [_selectButton setBackgroundImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
        _selectButton.tag = 2000;
        [UIView animateWithDuration:0.5 animations:^{
            _selectedView.frame = CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            _selectCollectionView.frame = CGRectMake(0, 0, SCREENWIDTH, 320 * FITHEIGHT);
        }];
    }
    else
    {
        [_selectButton setBackgroundImage:[UIImage imageNamed:@" 更多"] forState:UIControlStateNormal];
        _selectButton.tag = 1000;
        [UIView animateWithDuration:0.5 animations:^{
            _selectedView.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT - 64);
        }];
        [UIView animateWithDuration:0.5 animations:^{
            _selectCollectionView.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, 320 * FITHEIGHT);
        }];
    }
}

- (void)didClickedBackButton:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getSelectView
{
    _selectedView = [[UIView alloc] initWithFrame:CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT - 64)];
    _selectedView.backgroundColor = [UIColor blackColor];
    _selectedView.alpha = 0.7;
    _selectedView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapSelectView:)];
    [_selectedView addGestureRecognizer:tap];
    [self.view addSubview:_selectedView];
    
    UICollectionViewFlowLayout *selectCollectionViewFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    selectCollectionViewFlowLayout.itemSize = CGSizeMake((SCREENWIDTH - 26) / 3, 30 * FITHEIGHT);
    selectCollectionViewFlowLayout.minimumLineSpacing = 5;
    selectCollectionViewFlowLayout.minimumInteritemSpacing = 3;
    selectCollectionViewFlowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 5, 10);
    selectCollectionViewFlowLayout.scrollDirection = 0;
    
    self.selectCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, 320 * FITHEIGHT) collectionViewLayout:selectCollectionViewFlowLayout];
    _selectCollectionView.delegate = self;
    _selectCollectionView.dataSource = self;
    _selectCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_selectCollectionView];
    
    
    [_selectCollectionView registerClass:[YWSelectCollectionViewCell class] forCellWithReuseIdentifier:@"selectCell"];
}

- (void)didTapSelectView:(UITapGestureRecognizer *)tap
{
    [_selectCollectionView reloadData];
    [_selectButton setBackgroundImage:[UIImage imageNamed:@" 更多"] forState:UIControlStateNormal];
    _selectButton.tag = 1000;
    [UIView animateWithDuration:0.5 animations:^{
        _selectedView.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT - 64);
    }];
    [UIView animateWithDuration:0.5 animations:^{
        _selectCollectionView.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, 320 * FITHEIGHT);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _topListDataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YWHotTopListModel *hotListModel = [_topListDataArray objectAtIndex:indexPath.item];
    YWSelectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"selectCell" forIndexPath:indexPath];
    cell.hotListModel = hotListModel;
    if (indexPath.item == _didSelected) {
        cell.isSelect = YES;
    }
    else
    {
        cell.isSelect = NO;
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    _didSelected = indexPath.row;
    [_selectCollectionView reloadData];
    [_selectButton setBackgroundImage:[UIImage imageNamed:@" 更多"] forState:UIControlStateNormal];
    _selectButton.tag = 1000;
    [UIView animateWithDuration:0.5 animations:^{
        _selectedView.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, SCREENHEIGHT - 64);
    }];
    [UIView animateWithDuration:0.5 animations:^{
        _selectCollectionView.frame = CGRectMake(SCREENWIDTH, 0, SCREENWIDTH, 320 * FITHEIGHT);
    }];
    [_pageController moveToControllerAtIndex:indexPath.item animated:YES];
}





- (void)getData
{
    YWHotTopListModel *model = [[YWHotTopListModel alloc] init];
    model.key = @"1_21_ranking:album:subscribed:30:0";
    model.name = @"总榜";
    [_topListDataArray addObject:model];
    NSString *string = @"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/album?device=android&key=1_21_ranking:album:subscribed:30:0&pageId=1&pageSize=20&statEvent=pageview%2Franklis";
    [LTNetTool GetNetWithURL:string Body:nil Header:nil Response:LTJSON Success:^(id result) {
        NSArray *tempArray = [result objectForKey:@"categories"];
        for (NSDictionary *dic in tempArray) {
            YWHotTopListModel *hotListModel = [[YWHotTopListModel alloc] initWithDic:dic];
            [_topListDataArray addObject:hotListModel];
        }
        [_pageController reloadData];

        [_selectCollectionView reloadData];

    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
